package com.engine.jsit.duss.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.jsit.duss.utils.JsitUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-03-28
 * @Description: 信息学院同步接口
 * @ModifyLog:
 **/

public class WeaJsitWeb {
    /**
     * @Description: 获取文档列表
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/10
     */
    @GET
    @Path("/getDocList")
    @Produces({MediaType.TEXT_PLAIN})
    public String getDocList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        RecordSet rs = new RecordSet();
        String responeMsg = "";
        String responeCode = "";
        try {
            //通知公告：129,1541,3042；一周安排：131；学习交流：130；常用表格：133；
            String seccategorys = Util.null2String( request.getParameter( "seccategory" ) );//文档目录
            int rows = Util.getIntValue( request.getParameter( "rows" ),0 );//行数
            if (JsitUtil.getSeccategory( seccategorys )) {
                JSONArray arrayList = new JSONArray();
                rs.writeLog( ">>>>>getDocList>>>>>seccategorys=" + seccategorys+">>>>>rows="+rows );
                String sql = "select id,seccategory,docsubject,docextendname,doccreatedate from (" +
                        "select t1.id,t1.seccategory,t1.docsubject,t1.docextendname," +
                        "t1.doccreaterid,t1.ownerid,t1.docstatus,t1.doccreatedate from DocDetail t1  " +
                        "where  t1.docstatus != 8 " +
                        " and t1.docstatus != 9 and t1.docstatus <=9 and seccategory in ("+seccategorys+")" +
                        " and t1.secretLevel>=3 and (t1.ishistory is null or t1.ishistory = 0)" +
                        " and (t1.isreply is null or t1.isreply='' or t1.isreply='0') " +
                        " order by t1.doccreatedate desc)" +
                        " where rownum<="+rows;
                rs.executeQuery( sql);
                int counts = rs.getCounts();
                while (rs.next()) {
                    JSONObject jsonData = new JSONObject();
                    String id = Util.null2String( rs.getString( "id" ) );//文档ID
                    String docsubject = Util.null2String( rs.getString( "docsubject" ) );//文档标题
                    String seccategory = Util.null2String( rs.getString( "seccategory" ) );//文档目录
                    String doccreatedate = Util.null2String( rs.getString( "doccreatedate" ) );//文档创建日期
                    String url = "http://oa.jsit.edu.cn/spa/document/index.jsp?id=" + id + "&router=1#/main/document/detail";
                    jsonData.put( "docsubject", docsubject );
                    jsonData.put( "doccreatedate", doccreatedate );
                    jsonData.put( "url", url );
                    jsonData.put( "seccategory", seccategory );
                    arrayList.add( jsonData );
                }
                responeMsg = "成功返回列表,获取文档数量："+counts;
                responeCode = "001";
                apidatas.put( "data", arrayList );
            } else {
                responeMsg = "文档目录不存在";
                responeCode = "002";
            }
            apidatas.put( "responeMsg", responeMsg );
            apidatas.put( "responeCode", responeCode );
            rs.writeLog( "responeCode=" + responeCode + ",responeMsg=" + responeMsg );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "responeCode", "003" );
            apidatas.put( "responeMsg", "请求接口异常" );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 获取待办邮件列表
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/10
     */
    @GET
    @Path("/getMailList")
    @Produces({MediaType.TEXT_PLAIN})
    public String getMailList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        RecordSet rs = new RecordSet();
        String responeMsg = "";
        String responeCode = "";
        try {
            String workcode = Util.null2String( request.getParameter( "workcode" ) );//员工号
            int rows = Util.getIntValue( request.getParameter( "rows" ),0 );//行号
            rs.writeLog( ">>>>>>getMailList>>>>workcode=" + workcode+",rows="+rows );
            int hrmid = JsitUtil.getHrmidByWorkcode( workcode );
            if (hrmid > 0) {
                ResourceComInfo comInfo = new ResourceComInfo();
                JSONArray arrayList = new JSONArray();
                String sql = "select id,resourceid,subject,senddate from (\n" +
                        "select id,resourceid,subject,senddate from mailresource  \n" +
                        "where resourceid='"+hrmid+"' and status='0' order by senddate desc)\n" +
                        "where rownum<="+rows;
                rs.executeQuery( sql);
                int counts = rs.getCounts();
                while (rs.next()) {
                    JSONObject dataJson = new JSONObject();
                    String mailid = Util.null2String( rs.getString( "id" ) );
                    String resourceid = Util.null2String( rs.getString( "resourceid" ) );
                    String lastname = comInfo.getLastname( resourceid );
                    String subject = Util.null2String( rs.getString( "subject" ) );
                    String senddate = Util.null2String( rs.getString( "senddate" ) );
                    String dourl = "http://oa.jsit.edu.cn/spa/email/static/index.html#/main/email/view?id=" + mailid;
                    dataJson.put( "subject", subject );
                    dataJson.put( "sendname", lastname );
                    dataJson.put( "senddate", senddate );
                    dataJson.put( "dourl", dourl );
                    arrayList.add( dataJson );
                }
                responeMsg = "成功返回列表,获取待办邮件数量："+counts;
                responeCode = "001";
                apidatas.put( "data", arrayList );
            } else {
                responeMsg = "不存在员工号" + workcode;
                responeCode = "002";
            }
            apidatas.put( "responeMsg", responeMsg );
            apidatas.put( "responeCode", responeCode );
            rs.writeLog( "responeCode=" + responeCode + ",responeMsg=" + responeMsg );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "responeCode", "003" );
            apidatas.put( "responeMsg", "请求接口异常" );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 获取日程列表
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/10
     */
    @GET
    @Path("/getSchedulelist")
    @Produces({MediaType.TEXT_PLAIN})
    public String getSchedulelist(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        RecordSet rs = new RecordSet();
        String responeMsg = "";
        String responeCode = "";
        JSONArray arrayList = new JSONArray();
        String workcode = Util.null2String( request.getParameter( "workcode" ) );//员工号
        int rows = Util.getIntValue( request.getParameter( "rows" ),0 );//行号
        rs.writeLog( ">>>>>>getSchedulelist>>>>workcode=" + workcode+",rows="+rows );
        try {
            int userid = JsitUtil.getHrmidByWorkcode( workcode );
            if (userid > 0) {

                String sql = "select id,name,begindate,enddate,createrid,description,resourceid " +
                        "from WorkPlan where createrid='" + userid + "' and rownum<="+rows+
                        " order by begindate desc";
                rs.executeQuery( sql );
                int counts = rs.getCounts();
                while (rs.next()) {
                    JSONObject jsonData = new JSONObject();
                    jsonData.put( "name", Util.null2String( rs.getString( "name" ) ) );
                    jsonData.put( "begindate", Util.null2String( rs.getString( "begindate" ) ) );
                    jsonData.put( "enddate", Util.null2String( rs.getString( "enddate" ) ) );
                    jsonData.put( "description", Util.null2String( rs.getString( "description" ) ) );
                    String createrid = Util.null2String( rs.getString( "createrid" ));
                    jsonData.put( "createrid",JsitUtil.getLastnameById(createrid));
                    String resourceid = Util.null2String( rs.getString( "resourceid" ));
                    jsonData.put( "resourceid",JsitUtil.getLastnameById(resourceid));
                    arrayList.add( jsonData );
                }
                responeMsg = "成功返回列表,获取日程数量："+counts;
                responeCode = "001";
                apidatas.put( "data", arrayList );
            } else {
                responeMsg = "不存在员工号" + workcode;
                responeCode = "002";
            }
            apidatas.put( "responeMsg", responeMsg );
            apidatas.put( "responeCode", responeCode );

            rs.writeLog( "responeCode=" + responeCode + ",responeMsg=" + responeMsg );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "responeCode", "003" );
            apidatas.put( "responeMsg", "请求接口异常" );
        }
        return JSONObject.toJSONString( apidatas );
    }
    /**
     * @Description: 获取流程待办列表
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/10
     */
    @GET
    @Path("/getWorkflowDoinglist")
    @Produces({MediaType.TEXT_PLAIN})
    public String getWorkflowDoinglist(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        RecordSet rs = new RecordSet();
        String responeMsg = "";
        String responeCode = "";
        JSONArray arrayList = new JSONArray();
        String workcode = Util.null2String( request.getParameter( "workcode" ) );//员工号
        int rows = Util.getIntValue( request.getParameter( "rows" ),0 );//行号
        int scope = Util.getIntValue( request.getParameter( "scope" ),0 );//0：所有流程待办；1：收文、发文待办；2：除收文、发文的待办
        rs.writeLog( ">>>>>>getWorkflowDoinglist>>>>workcode=" + workcode+",rows="+rows );
        try {
            int userid = JsitUtil.getHrmidByWorkcode( workcode );
            if (userid > 0) {
                String sqlwhere = " WHERE t1.requestid = t2.requestid AND t1.workflowid = t3.ID ";
                sqlwhere+= " AND t2.userid IN ("+userid+")";
                sqlwhere+= " AND t2.usertype = 0";
                sqlwhere+=  " AND (NVL (t1.currentstatus ,- 1) = - 1 OR (NVL (t1.currentstatus ,- 1) = 0 AND t1.creater IN ("+userid+")))";
                sqlwhere+= " AND (t1.deleted <> 1 OR t1.deleted IS NULL OR t1.deleted = '')";
                sqlwhere+= " AND ((t2.isremark = '0' AND (t2.takisremark IS NULL OR t2.takisremark = 0)) OR t2.isremark IN ('1', '5', '8', '9', '7', '11'))";
                sqlwhere+= " AND t2.islasttimes = 1";
                sqlwhere+= " AND (isprocessing = '' OR isprocessing IS NULL)";
                sqlwhere+= " AND t3.isvalid IN ('1', '3')";

                if(scope==1){
                    sqlwhere+= " AND t1.workflowid IN (4521,521)";
                }
                if(scope==2){
                    sqlwhere+= " AND t1.workflowid NOT IN (4521,521)";
                }

                String groupby = " ORDER BY receivedate DESC";
                String sql = "SELECT requestid,createdate,creater,workflowid,requestname " +
                        " FROM (select t1.requestid,t1.createdate,t1.creater,t1.workflowid,t1.requestname" +
                        " from workflow_requestbase t1,workflow_currentoperator t2,workflow_base t3" +
                        sqlwhere+groupby+") where rownum<="+rows;
                rs.executeQuery( sql );
                int counts = rs.getCounts();
                while (rs.next()) {
                    JSONObject jsonData = new JSONObject();
                    String requestid = Util.null2String( rs.getString( "requestid" ) );
                    jsonData.put( "requestname", Util.null2String( rs.getString( "requestname" ) ) );
                    jsonData.put( "createdate", Util.null2String( rs.getString( "createdate" ) ) );
                    String creater = Util.null2String( rs.getString( "creater" ));
                    jsonData.put( "creater",JsitUtil.getLastnameById(creater));
                    String link = "http://oa.jsit.edu.cn/spa/workflow/static4form/index.html?#/main/workflow/req?requestid="+requestid;
                    jsonData.put( "link",link);
                    arrayList.add( jsonData );
                }
                responeMsg = "成功返回列表,获取待办流程数量："+counts;
                responeCode = "001";
                apidatas.put( "data", arrayList );
            } else {
                responeMsg = "不存在员工号" + workcode;
                responeCode = "002";
            }
            apidatas.put( "responeMsg", responeMsg );
            apidatas.put( "responeCode", responeCode );

            rs.writeLog( "responeCode=" + responeCode + ",responeMsg=" + responeMsg );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "responeCode", "003" );
            apidatas.put( "responeMsg", "请求接口异常" );
        }
        return JSONObject.toJSONString( apidatas );
    }

}
